export const firstConfirm = [{
    type: 'confirm',
    name: 'confirmed',
    message: '欢迎使用打包脚本，在使用前请确保相关应用已经切换至对应的打包分支且已是最新代码?',
    default: true, // 默认为 Yes (Y)
}]
// 印章平台涉及的应用信息
const sealProjectList = [
    { name: 'sign-sz-front', checked: true, value: 'baseh5' },
    { name: 'seal-open-web', checked: true, value: 'open' },
    { name: 'seal-platform-base-front', checked: true, value: 'realNameSealBase' },
    { name: 'seal-platform-web', value: 'manage' },
    { name: 'local-sign-front', value: 'local-sign-front' }
]
// 对应的插件应用
const pluginsSealProjectList = sealProjectList.map(project => {
    return {
        name: `${project.name}-plugins`,
        value: `${project.value}-plugins`
    }
})

export const questions = [
    {
        type: 'list',
        name: 'project',
        message: '选择你要打包的项目?',
        choices: [{ name: '海南', checked: true, value: 'haiNan' }, { name: '江西', value: 'jiangXi' }],
        default: 'haiNan'
    },
    {
        type: 'list',
        name: 'env',
        message: '选择要打包的环境?',
        choices: ['test', 'prod'],
        default: 'test'
    },
    {
        type: 'checkbox',
        name: 'apply',
        message: '请选择需要打包的应用',
        choices: sealProjectList,
        validate: function (answer) {
            if (answer.length < 1) {
                return '您必须至少选择一个应用进行打包。'
            }
            return true;
        }
    },
    {
        type: 'input',
        name: 'dirPath',
        message: '请输入打包应用在本机的路径',
        default: '',
    },
]
// 已选项目路径
export const projectPathsFn = (dirPath, apply, project = sealProjectList) => {
    const choseProject = project.filter(item => apply.includes(item.value))
    return choseProject.reduce((accumulator, currentItem) => {
        accumulator[currentItem.value] = `${dirPath}/${currentItem.name}`
        return accumulator
    }, {})
}
/**
 * 海南项目打包的执行命令
 */
const getHaiNanCommandsFn = (answers) => {
    const { env, dirPath, apply } = answers
    const projectPaths = projectPathsFn(dirPath, apply)
    // 根据选择的应用进行打包
    return apply.map(apy => {
        return {
            name: apy,
            command: `cd ${projectPaths[apy]} && yarn && yarn build:${env}HN`
        }
    })

}
const jiangXiCommandsFn = ({projectPaths, apy, env, dirPath}) => {
    
    const enCom = env === 'test' ? 'prodJXTest' : `prodJX`
    let buildCom = ''
    if (apy.includes('plugins')) {
        buildCom = `pnpm run build:plugin -- --mode=${enCom}`
    } else {
        buildCom = `pnpm run build:${enCom} && yarn plugin:cp -f ${projectPaths[apy]}-plugins/${apy} -t ./${apy} --cwd ./`
    }       
    return  `cd ${projectPaths[apy]} && pnpm i && ${buildCom}`
}

const applyGetCommandFnMap = (...args) => {
    return {
        jiangXi: jiangXiCommandsFn(...args)
    }
}
/**
 * 江西项目打包的执行命令
 */
export const getPluginsCommands = (answers) => {
    const { env, dirPath, apply, project } = answers
    // 插件应用的value集合
    const applyPlugins = apply.map(ap => `${ap}-plugins`)
    // 插件应用需要增加对应的插件应用进行打包
    const commandPluginApply = pluginsSealProjectList.filter(item => applyPlugins.includes(item.value)).map(pro => pro.value)
    
    const commandApply = [ ...commandPluginApply, ...apply]
    const projectPaths = projectPathsFn(dirPath, commandApply, [...pluginsSealProjectList, ...sealProjectList])

    return commandApply.map(apy => {
        return {
            name: apy,
            command: applyGetCommandFnMap({projectPaths, apy, env, dirPath})[project]
        }
    })
    
}
const projectCommandMap = (answers) => {

    return {
        haiNan: getHaiNanCommandsFn(answers),
        jiangXi: getPluginsCommands(answers)
    }
}
// 执行的打包命令
export const buildCommandsFn = (answers) => {
    const { project } = answers
    return projectCommandMap(answers)[project]
}

